import numpy as np


def handle_binary_image_2(image_array):
    top_row = image_array[0, :]; bottom_row = image_array[-1, :]
    width = image_array.shape[1]; height = image_array.shape[0]

    image_array_0 = np.append(top_row.reshape(1, -1), image_array, axis=0)
    image_array_0 = np.append(image_array_0, bottom_row.reshape(1, -1), axis=0)

    left_column_0 = image_array_0[:, 0]; right_column_0 = image_array_0[:, -1]
    left_column = left_column_0.reshape(-1, 1); right_column = right_column_0.reshape(-1, 1)
    image_array_0 = np.hstack((left_column, image_array_0, right_column))

    offsets = np.array([        [0, 1], [0, 2],
                        [1, 0], [1, 1], [1, 2], [1, 3],
                        [2, 0], [2, 1], [2, 2], [2, 3],
                                [3, 1], [3, 2]])

    all_image_list = {}
    names = np.array(['top0', 'top1',
                      'left0', 'this00', 'this01', 'right0',
                      'left1', 'this10', 'this11', 'right1',
                      'bottom0', 'bottom1'])
    for i in range(len(offsets)):
        offset = offsets[i]
        name = names[i]
        image_array_local = image_array_0[offset[0]:offset[0] + height - 1, offset[1]:offset[1] + width - 1]

        all_image_list[name] = image_array_local

    a = all_image_list

    right_start = [0, 0]; right_up_start = [0, 0]; right_down_start = [-1, 0]
    right_indices_tuple = np.where((a['this00'] == 1) & (a['this01'] == 1) & (a['top0'] == 0) & (a['top1'] == 0)) #start: [0, 0]
    right_up_indices_tuple = np.where((a['this00'] == 1) & (a['this01'] == 1) & (a['top0'] == 0) & (a['top1'] == 1)) #start: [0, 0]
    right_down_indices_tuple = np.where((a['this00'] == 1) & (a['this01'] == 1) & (a['top0'] == 1) & (a['top1'] == 0)) #start: [0, -1]

    right_indices = np.array(right_indices_tuple)
    right_up_indices = np.array(right_up_indices_tuple)
    right_down_indices = np.array(right_down_indices_tuple)
    right_indices[:, 0] += right_start[0]; right_indices[:, 1] += right_start[1]
    right_up_indices[:, 0] += right_up_start[0]; right_up_indices[:, 1] += right_up_start[1]
    right_down_indices[:, 0] += right_down_start[0]; right_down_indices[:, 1] += right_down_start[1]


    down_start = [0, 1]; down_right_start = [0, 1]; down_left_start = [0, 2]
    down_indices_tuple = np.where((a['this01'] == 1) & (a['this11'] == 1) & (a['right0'] == 0) & (a['right1'] == 0))
    down_right_indices_tuple = np.where((a['this01'] == 1) & (a['this11'] == 1) & (a['right0'] == 0) & (a['right1'] == 1))
    down_left_indices_tuple = np.where((a['this01'] == 1) & (a['this11'] == 1) & (a['right0'] == 1) & (a['right1'] == 0))

    down_indices = np.array(down_indices_tuple)
    down_right_indices = np.array(down_right_indices_tuple)
    down_left_indices = np.array(down_left_indices_tuple)
    down_indices[:, 0] += down_start[0]; down_indices[:, 1] += down_start[1]
    down_right_indices[:, 0] += down_right_start[0]; down_indices[:, 1] += down_right_start[1]
    down_left_indices[:, 0] += down_left_start[0]; down_indices[:, 1] += down_left_start[1]

    left_start = [1, 1]; left_down_start = [1, 1]; left_right_start = [2, 1]
    left_indices_tuple = np.where((a['this11'] == 1) & (a['this10'] == 1) & (a['bottom1'] == 0) & (a['bottom0'] == 0))
    left_down_indices_tuple = np.where((a['this11'] == 1) & (a['this10'] == 1) & (a['bottom1'] == 0) & (a['bottom0'] == 1))
    left_up_indices_tuple = np.where((a['this11'] == 1) & (a['this10'] == 1) & (a['bottom1'] == 1) & (a['bottom0'] == 0))

    left_indices = np.array(left_indices_tuple)
    left_down_indices = np.array(left_down_indices_tuple)
    left_up_indices = np.array(left_up_indices_tuple)
    left_indices[:, 0] += left_start[0]; left_indices[:, 1] += left_start[1]
    left_down_indices[:, 0] += left_down_start[0]; left_down_indices[:, 1] += left_down_start[1]
    left_up_indices[:, 0] += left_right_start[0]; left_up_indices[:, 1] += left_right_start[1]

    up_start = [1, 0]; up_left_start = [1, 0]; up_right_start = [1, -1]
    up_indices_tuple = np.where((a['this10'] == 1) & (a['this00'] == 1) & (a['left1'] == 0) & (a['left0'] == 0))
    up_left_indices_tuple = np.where((a['this10'] == 1) & (a['this00'] == 1) & (a['left1'] == 0) & (a['left0'] == 1))
    up_right_indices_tuple = np.where((a['this10'] == 1) & (a['this00'] == 1) & (a['left1'] == 1) & (a['left0'] == 0))
    up_indices = np.array(up_indices_tuple)
    up_left_indices = np.array(up_left_indices_tuple)
    up_right_indices = np.array(up_right_indices_tuple)
    up_indices[:, 0] += up_start[0]; up_indices[:, 1] += up_start[1]
    up_left_indices[:, 0] += up_left_start[0]; up_left_indices[:, 1] += up_left_start[1]
    up_right_indices[:, 0] += up_right_start[0]; up_indices[:, 1] += up_right_start[1]
    t = 12
